<?php
/**
 * Created by PhpStorm.
 * User: change
 * Date: 2017/3/1
 * Time: 22:33
 */

namespace Library;

use JPush\Client;
use JPush\Exceptions\APIConnectionException;
use JPush\Exceptions\APIRequestException;
use JPush\Exceptions\JPushException;

require 'jpush_sdk/autoload.php';

class jPush
{
    //患者key
    private $p_app_key = '2d8580294b0476c183bc2ace';
    private $p_master_secret = '6b645597e9c114559ac836df';
    //医生key
    private $d_app_key = '6f40888a4e6525a6758ea831';
    private $d_master_secret = 'f744272fc47271225f723045';

    protected $client;
    protected $mobile;

    public function __construct()
    {
        $this->mobile = new Mobile();
    }

    /**
     * @param $user_id   ---用户名
     * @param int $user_type   ---用户类型
     * @param $api_info   ---接口信息 controller:控制器名 action:方法名
     * @param $msg   ---消息内容
     * @param string $content   ---消息描述
     * @param array $data   ---数据
     * @param string $device   ---设备类型
     * 推送消息
     */
    public function pushMsg($user_id,$user_type,$api_info,$msg,$content='',$data=array(),$device=''){
        $user_name = '';
        $data = empty($data) ? null : $data;
        //患者端
        if ($user_type == 1){
            $this->client = new Client($this->p_app_key,$this->p_master_secret);
            $user_name = 'ysp'.$user_id;
        }
        //医生端
        if ($user_type == 2){
            $this->client = new Client($this->d_app_key,$this->d_master_secret);
            $user_name = 'ysd'.$user_id;
        }
        //获取手机类型
        $phone_type = $this->mobile->getPhoneClass();
//        if ($phone_type) {
//            $this->client->push()
//                ->addAlias($user_name)
//                ->message($msg, [
//                    'title' => $msg,
//                    'content_type' => 'text',
//                    'extras' => [
//                        'content' => $content
//                    ]
//                ]);
//        }
        $phone_type = 'iOS';
        if ($phone_type){
            $phone_type = empty($device) ? $phone_type : $device;
            try {
    //            $response = $push_payload->send(); // 执行推送
                $pusher = $this->client->push();
                $pusher->setPlatform($phone_type);
                $pusher->addAlias($user_name);
//                $pusher->addAllAudience();
//                $pusher->setNotificationAlert($msg);
                //消息
    //            $pusher->setSmsMessage('hi..hi',10);
//                $pusher->message('消息',array('title'=>$msg));
                //通知内容
                $extras=array(
                    'controller' => $api_info['controller'],
                    'action' => $api_info['action'],
                    'content' => $content,
                    'data' => $data
                );
                //安卓自定义
                $android_notification = array(
//                    "alert"=>$msg,
                    "title"=>$msg,
                    "builder_id"=>1,
                    "extras"=>$extras
                );
                //ios自定义
                $ios_notification = array(
//                    "alert"=>$msg,
                    "badge"=>"1",
                    "sound"=>"default",
                    "extras"=>$extras
                );
                //发送通知
                switch ($phone_type){
                    case 'Android':
                        $pusher->androidNotification($msg,$android_notification);
                        break;
                    case 'iOS':
                        //$pusher->addRegistrationId($user_name);
                        //$pusher->setNotificationAlert($msg);
                        //$pusher->options(['apns_production'=>true]);   //苹果生产环境
                        $pusher->iosNotification($msg,$ios_notification);
                        //$pusher->build();
                        break;
                    case 'all':
                        $pusher->androidNotification($msg,$android_notification);
                        $pusher->iosNotification($msg,$ios_notification);
                        break;
                }
                //推送消息
                $pusher->send();
            }catch (APIConnectionException $e) { // 请求异常
                // try something here
//                print $e;
            } catch (APIRequestException $e) { // 回复异常
                // try something here
//                print $e;
            }
        }
    }

    /**
     * 发送消息--异常处理
     */
    public function sendMsgByTry(){
        $this->client = new Client($this->p_app_key,$this->p_master_secret);
        $pusher = $this->client->push();
        $pusher->setPlatform('android,ios');
        $pusher->addAlias(array('ysp156'));
        $pusher->options(array(
            "apns_production" => true  //true表示发送到生产环境(默认值)，false为开发环境
        ));
        $pusher->addAllAudience();
        $pusher->setNotificationAlert('Hello, JPush-----Kuler');
        try {
            $pusher->send();
        } catch (JPushException $e) {
            // try something else here
            print $e;
        }
    }

}